home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / MOUSE_UT / MOUSTOOL / MOUSTOOL.DOC < prev    next >
Text File  |  1993-01-04  |  19KB  |  438 lines

  1. ╔═══════════════════════════════════════════════════════════════════════════╗
  2. ║                                                                           ║
  3. ║                               MouseTools                                  ║
  4. ║                       Version 1.0    April 7, 1989                        ║
  5. ║                                                                           ║
  6. ║                              Nels Anderson                                ║
  7. ║                             92 Bishop Drive                               ║
  8. ║                          Framingham, MA  01701                            ║
  9. ║                                                                           ║
  10. ╚═══════════════════════════════════════════════════════════════════════════╝
  11.  
  12. The use of a mouse as an input device is becoming increasingly common in
  13. many types of programs.  While this (usually) makes life easier for the
  14. user, it certainly does make the programmer's job harder.  Several things
  15. I've worked on seemed to demand a mouse interface, so I ended up writing
  16. my own utilities to use a mouse and I present them in this package for
  17. you to use in your own Turbo Pascal 5.0 programs.
  18.  
  19. This set of utilities provides most of the necessary procedures to use
  20. a mouse in your own programs, including:
  21.  
  22.   o  display the mouse cursor
  23.   o  read mouse and button positions
  24.   o  display various prompts with "push buttons"
  25.   o  select filenames
  26.  
  27. To use these tools you will need Turbo Pascal 5.0 and you must have the
  28. TP5 units Crt, Dos, Drivers, Fonts and Graph available.  I've provided you
  29. with two sample programs (mapedit.pas, cgaexp.pas) along with the .tpu
  30. files for all the mouse tools.  This should give you enough to get started
  31. writing mouse programs in Turbo Pascal.  The source code for the units is
  32. not included.
  33.  
  34. MouseTools is provided as Shareware.  You may use it, copy it for friends,
  35. upload it to BBS's, etc.  If you continue to use MouseTools and would like
  36. to reward me for my work, you can send a donation ($10 suggested) to me
  37. at:
  38.  
  39.                              Nels Anderson
  40.                             92 Bishop Drive
  41.                          Framingham, MA  01701
  42.  
  43. Registered users will get the latest updates on the tools as well as the
  44. source code for all the mouse units.  Support is also available through
  45. my BBS, )(evious, at 508-875-3618 or 617-449-7322, 300/1200/2400/9600HST.
  46. I'd love to see anything you create using these tools; if you can either
  47. mail me a copy or upload it to the BBS.
  48.  
  49. If you're connected to one of the minicomputer nets (InterNet, uucp...)
  50. you can try reaching me at one of these addresses:
  51.  
  52.                 ima!primerd!en-m32.prime.com!nja
  53.                 uunet!en-m32.prime.com!nja
  54.                 csnet-relay!en-m32.prime.com!nja
  55.                 nja%en-m32.prime.com@relay.cs.net
  56.                 nja@en-m32.prime.com
  57.  
  58.  
  59. ╔═══════════════════════════════════════════════════════════════════════════╗
  60. ║                            Using MouseTools                               ║
  61. ╚═══════════════════════════════════════════════════════════════════════════╝
  62.  
  63. Probably the best way to understand how the tools work is to examine the
  64. sample programs, mapedit.pas and cgaexp.pas.  You should refer to them for
  65. actual examples of how things are used as they are described here.
  66.  
  67. First, some basic concepts.  Most of your interface to the mouse is
  68. actually done by the mouse driver that is supplied by the mouse manu-
  69. facturer.  The interface to the mouse driver is done through interrupts
  70. similar to the way MS-DOS or BIOS functions are accessed.  If you've
  71. never used MS-DOS/BIOS functions, don't worry.  MouseTools handles all
  72. the messy details for you.
  73.  
  74. Once the mouse is activated and you can read positions from it, your
  75. program needs to display something on the screen to echo the mouse's
  76. actions.  MouseTools allows you to use any shape as a mouse cursor,
  77. and comes with an arrow, finger and hand cursor already drawn.  With
  78. the registered version you can add your own cursor shapes.  Tools are
  79. provided for displaying and moving the cursor around the screen.
  80.  
  81. In addition to movement, you need to determine when the user wants to
  82. do something either by just being over a particular part of the screen
  83. or by using the mouse buttons.  These are the basic tools of the mouse
  84. interface.
  85.  
  86. In addition to the basics, tools are provided to display prompts of
  87. several types and return the selections the user made.  One of the most
  88. sophisticated prompts is the file selection tool, which allows the user
  89. to select files through a scrolling list and includes the ability to
  90. change directories and drives.
  91.  
  92. OK, so all these tools are available.  Now you're probably wondering
  93. exactly how to use them.  Let's go through that step by step.
  94.  
  95. INITIALIZATION
  96.  
  97. First, you must enter graphics mode as you would in any Turbo Pascal
  98. program.   Then the mouse itself must be initialized.  The functions
  99. to do this are:
  100.  
  101. MReset -- resets the mouse and returns -1 if a mouse driver is installed
  102. MLimit (x1,x2,y1,y2) -- sets the limits of the mouse coordinates
  103. MPut(x,y) -- positions the mouse at coordinates x,y
  104.  
  105. If MReset doesn't return a value of -1 there is no mouse driver installed
  106. and your program won't be able to do any mouse functions.  MLimit is used
  107. to control the range of values the mouse returns to you.  These limits
  108. will probably be equal to the size of the screen (i.e., x=0 thru 639,
  109. y=0 thru 349 for EGA graphics mode).  MPut sets the mouse coordinates to
  110. start from.  You'll probably want to either put the mouse in a corner or
  111. in the middle of the screen.
  112.  
  113. Several Pascal variables are used to keep track of what the mouse has
  114. been doing.  These are:
  115.  
  116. Mx -- last mouse x coordinate        \
  117. My -- last mouse y coordinate         >  all are in mouse.tpu
  118. Button -- last mouse button value    /
  119.  
  120. Initially, you should set Mx and My to the values you used in MPut(x,y),
  121. and you should initialize Button to 0.
  122.  
  123. You'll also need some memory on the heap to save the screen that's under
  124. the mouse cursor.  The variable MCurs (in mouse.tpu) is used to point to
  125. this memory.  Set it up by using:
  126.  
  127. GetMem(MCurs,ImageSize(0,0,MW,MH));    {reserve memory for mouse cursor}
  128.  
  129. There's still no visible indication on the screen that there's a mouse
  130. in use.  You can now turn on the mouse cursor using the procedure:
  131.  
  132. MouseCursorOn(Mx,My,HAND);        {display a hand cursor}
  133.  
  134. The first two parameters determine where the mouse cursor will be.
  135. The third parameter determines the shape of the mouse cursor.  The
  136. available cursor shapes are HAND, FINGER and ARROW.
  137.  
  138. Now everything is set, and you can start the main body of your program.
  139.  
  140.  
  141. USING THE MOUSE IN YOUR MAIN PROGRAM
  142.  
  143. The main body of your program will need a loop where the mouse is
  144. polled allowing its displayed position to be updated and to check
  145. for command requests from the user.
  146.  
  147. The main procedure in this loop is:
  148.  
  149. MStatus(NewButton,NewX,NewY);            {get mouse status}
  150.  
  151. This returns the current position of the mouse buttons followed by the
  152. x and y mouse coordinates.  There are several things you can then do
  153. with this information:
  154.  
  155.  *  Compare NewX vs. Mx and NewY vs. My to see if the mouse has moved.
  156.     If it has, use:
  157.     
  158.     MouseCursor(NewX,NewY,Mx,My,HAND);        {move mouse cursor}
  159.     
  160.     to move the mouse cursor to its new position.  After moving, save NewX
  161.     into Mx and NewY into My.
  162.  
  163.  *  See if the mouse is over something significant by using:
  164.  
  165.     MouseLocate(NewX,NewY,18,@mt)
  166.  
  167.     This function uses a table ("mt" in this case, containing 18 items)
  168.     of coordinates and returns a value indicating which if any sets of
  169.     coordinates NewX/NewY are within.  MouseLocate() is further described
  170.     later.
  171.  
  172.  *  See if a button is being pushed by comparing NewButton with
  173.     Button.  Detecting a button push will usually mean a call to
  174.     MouseLocate() to find what item the user is selecting.  Then
  175.     a case statement can be used to call the appropriate procedure.
  176.  
  177. Believe it or not, that's basically it!  The remainder of this file will
  178. describe in detail what all the available functions and procedures do.
  179.  
  180.  
  181. MOUSETOOLS DETAILED DESCRIPTIONS
  182.  
  183. Once you understand the basics of initializing the mouse and detecting
  184. what it's doing in the body of the program, you'll need to know what
  185. tools are available for using it further.  This section describes in
  186. detail all the available mouse functions and procedures, in order by
  187. the unit they are in.
  188.  
  189. MOUSE.TPU
  190.  
  191. This unit provides the interface to the mouse driver, plus the basic
  192. routines for displaying the mouse cursor and determining what the
  193. mouse is pointing to.
  194.  
  195. function MReset:  INTEGER;
  196.  
  197.   This function initializes the mouse.  It returns an integer value of
  198.   -1 if a mouse is detected.  If MReset does not return -1, you cannot
  199.   use mouse functions in the remainder of the program.
  200.   
  201. procedure MPut(mx,my:  INTEGER);
  202.  
  203.   This procedure sets the current mouse position to mx,my.  That is, if
  204.   you were to immediately read the mouse position using MStatus() before
  205.   the user had a chance to move it, the values you'd receive back would
  206.   be mx,my.
  207.  
  208. procedure MStatus(var button,xpos,ypos:  INTEGER);
  209.  
  210.   This procedure returns the current state of the mouse.  "Button" is a
  211.   bit mapped value indicating which buttons are pressed (i.e., Button is
  212.   1 if button 1 is pressed, 2 if button 2 is pressed, 3 if both buttons
  213.   1 and 2 are pressed).  xpos,ypos are the current position of the mouse.
  214.  
  215. procedure MLimit(xmin,xmax,ymin,ymax:  INTEGER);
  216.  
  217.   You can limit the coordinate values returned by MStatus using this
  218.   procedure.  To prevent the mouse from going off the screen in EGA
  219.   640x350 mode, you could set xmin=0, xmax=639, ymin=0, ymax=349.
  220.   Smaller values can be used to limit mouse travel to only a portion
  221.   of the screen.
  222.  
  223. procedure MouseCursor(x,y,Oldx,Oldy,Num:  INTEGER);
  224.  
  225.   This procedure moves the displayed mouse cursor from one position, 
  226.   Oldx,Oldy to a new position, x,y.  Make sure the mouse cursor is
  227.   actually being displayed when you call this function, otherwise you
  228.   will leave blocks of garbage on the screen.
  229.  
  230. procedure MouseCursorOn(x,y,Num:  INTEGER);
  231.  
  232.   This procedure is basically one half of MouseCursor().  Use it when
  233.   the mouse cursor is not currently being displayed but now should be,
  234.   such as at the beginning of the program.  MouseCursorOn() must be
  235.   used before MouseCursor() is first called.
  236.  
  237. procedure MouseCursorOff(Oldx,Oldy:  INTEGER);
  238.  
  239.   This procedure removes the mouse cursor from the screen and restores
  240.   whatever was under it to view.
  241.  
  242. function MouseLocate(x,y,size:  INTEGER; mt:  mtptr):  INTEGER;
  243.  
  244.   This function is the key to making the mouse do useful things within
  245.   your program.  It uses a table "mt" with "size" entries in it to
  246.   determine what the user is pointing to.  "x" and "y" should be the
  247.   current mouse coordinates (Mx and My).
  248.   
  249.   To use MouseLocate you must set up a table of integers, 4 numbers
  250.   per entry, which describe rectangles on the screen.  Here is a sample
  251.   table:
  252.   
  253.   Const
  254.     mt: array[1..2,1..4] of INTEGER = (
  255.     (400,620,68,81),            {save}
  256.     (400,620,82,95) );            {read}
  257.   
  258.   The sample shows two possible functions, save and read, that the user
  259.   can select.  If the x coordinate of the mouse is between 400 and 620,
  260.   and the y coordinate is between 68 and 81 then "save" is being
  261.   selected.  MouseLocate will check all entries in the table and will
  262.   return the number of the first table entry found that contains the
  263.   specified coordinates.
  264.   
  265.   Note that when calling MouseLocate you must actually pass a pointer
  266.   to the table rather than the table itself, so a sample call to
  267.   MouseLocate might look like this:
  268.   
  269.   i := MouseLocate(Mx,My,18,@mt);
  270.  
  271. MOUSERS2.TPU
  272.  
  273. This unit provides several ways of prompting the user for input and
  274. using the mouse to get his response.
  275.  
  276. function MGetFile(FileSpec,Heading:  STRING): STRING;
  277.  
  278.   This is a very sophisticated routine for allowing the user to select
  279.   file names.  A box is popped up containing a list of files that the
  280.   user can select from.  The list can be scrolled if all valid files
  281.   do not fit and the user can switch between directories and disks.
  282.   "FileSpec" specifies what file names will be shown.  You'd use "*.*"
  283.   to show all files, but if you only wanted files with the suffix "pic"
  284.   you could set FileSpec to "*.pic".  Many possibilities are available
  285.   for FileSpec.  "Heading" is a prompt to the user that will be at the
  286.   top of the box showing the file names.  MGetFile returns a string
  287.   containing the filename that was selected.  If character 0 of the
  288.   returned string is equal to #255 however the user aborted the file
  289.   selection.
  290.  
  291. function MouseYN(x,y: INTEGER; Heading:  STRING):  BOOLEAN;
  292.  
  293.   This function displays a question contained in "Heading" along with
  294.   a yes and no button.  If the user selects the yes button, the
  295.   function returns a TRUE value, otherwise it returns false.
  296.   
  297. function MouseQuestion(Size: INTEGER;Heading: STRING;Ques: QTable): INTEGER;
  298.  
  299.   This function displays a question and a list of answers for the user
  300.   to choose from, returning the number of the user's choice.  "Size"
  301.   is the number of possible answers.  "Ques" is an array of strings
  302.   containing the possible responses.  A sample "Ques" might look like:
  303.   
  304.   PalQues: array[1..5] of STRING = (    {palette questions}
  305.   'Save','Load','Change','Rotate','Default');
  306.   
  307.   Note that when calling MouseQuestion you must actually pass a pointer
  308.   to the table rather than the table itself, so a sample call to
  309.   MouseQuestion might look like this:
  310.   
  311.   case MouseQuestion(5,'Select a palette function',@PalQues) of
  312.  
  313. function MouseReadKey(Heading: STRING): CHAR;
  314.  
  315.   This function displays a heading (actually, the heading is optional)
  316.   and waits for the user to hit a single key or click the mouse.  If
  317.   a key is hit, its value is returned; otherwise if the mouse is
  318.   clicked a value of #0 is returned.
  319.  
  320. BOX.TPU
  321.  
  322. This unit has a couple of tools used for popup boxes and other things.
  323.  
  324. procedure OutlineBox(x1,y1,x2,y2,boxcolor,rectcolor:  INTEGER);
  325.  
  326.   This procedure draws a box using "boxcolor" as the background color
  327.   of the box, and using a single pixel wide outline of color "rectcolor"
  328.   around the edge.  This is the standard box used for mouse prompts.
  329.  
  330. procedure XPutPixel(x,y,color:  INTEGER);
  331.  
  332.   Plot a pixel at x,y by XOR'ing its current color with "color".
  333.  
  334. procedure XLine(x1,y1,x2,y2,color:  INTEGER);
  335.  
  336.   Draw a line from x1,y1 to x2,y2 by XOR'ing its current color
  337.   with "color".
  338.  
  339. procedure Input(var temp:  STRING);
  340.  
  341.   String input routine for graphics mode.  Handles echoing of the
  342.   string using the currently selected font as well as displaying a
  343.   cursor and backspacing.
  344.  
  345. PALETTE.TPU
  346.  
  347. This unit is not strictly part of the mouse tools, but is needed to
  348. use EGA graphics mode.  Its only procedure is one to initialize graphics
  349. mode.
  350.  
  351. procedure Initialize;
  352.  
  353.   As currently set up, Initialize sets up EGA 640x350 mode graphics.
  354.   This routine is right out of the Turbo Pascal sample programs, so if
  355.   you need other modes you should be able to make your own version of
  356.   it.
  357.  
  358. CONVERT.TPU
  359.  
  360. This unit isn't really necessary for a mouse interface either, but
  361. contains some functions that I use in mapedit.pas and which you might
  362. find useful.
  363.  
  364. function RtoI(number:  REAL): INTEGER;
  365.  
  366.   Takes the real variable "number" and returns it as an integer.
  367.  
  368. function ItoS(number:  LONGINT): STRING;
  369.  
  370.   Takes the integer variable "number" and returns it as a string.
  371.  
  372. function ItoFS(number:  LONGINT;  a:  INTEGER; fill:  CHAR): STRING;
  373.  
  374.   Takes the integer variable "number" and returns it as a formatted
  375.   string, exactly "a" characters long with unneeded character positions
  376.   set to the character "fill".
  377.  
  378. function RtoS(number:  REAL; a,b:  INTEGER): STRING;
  379.  
  380.   Takes the real variable "number" and returns it as a formatted
  381.   string, exactly "a" characters long with "b" characters after the
  382.   decimal point.
  383.  
  384. function NoSpace(s: STRING): STRING;
  385.  
  386.   Takes the string "s" and returns it with leading spaces removed.
  387.  
  388. function GetVal(s: STRING): REAL;
  389.  
  390.   Takes the string "s" and returns it as a real number.
  391.  
  392.  
  393. ╔═══════════════════════════════════════════════════════════════════════════╗
  394. ║                            Sample Programs                                ║
  395. ╚═══════════════════════════════════════════════════════════════════════════╝
  396.  
  397. MapEdit, one sample program used to demonstrate MouseTools, is actually
  398. a utility I wrote for a friend who needed to be able to draw icons that
  399. would be combined to create a map display in a program he was writing.
  400. Another friend has since used MapEdit to draw the keys for a calculator
  401. program he is writing.  I haven't used any MapEdit icons in any released
  402. programs *yet* but I'm working on an adventure game that will use maps
  403. and creatures created with MapEdit.  So, in addition to a sample of the
  404. mouse tools you're getting a potentially useful program since there are
  405. lots of possible uses for graphical icons.
  406.  
  407. I've included a sample icon file called ccmap.pic for you to play around
  408. with.  This is some of the icons for my future adventure game.  Try using
  409. the various functions of MapEdit to see what the mouse tools actually 
  410. look like on the screen.
  411.  
  412. By the way, if you want to see an actual program that used MapEdit, look
  413. for BassTour by Dick Olsen, currently available on BBS's as BASSTR25.ZIP.
  414.  
  415. CGAExp, the other sample program, doesn't actually do anything useful but
  416. demonstrates that the MouseTools can be used in CGA graphics as well as
  417. EGA.  Since there's less actual program surrounding the tools, you might
  418. find it more useful for understanding the tools than MapEdit.
  419.  
  420. ╔═══════════════════════════════════════════════════════════════════════════╗
  421. ║                             Included Files                                ║
  422. ╚═══════════════════════════════════════════════════════════════════════════╝
  423.  
  424. The following files are included in MouseTools:
  425.  
  426. MOUSE.TPU       Mouse driver interface and other basic mouse tools
  427. MOUSERS2.TPU    Mouse response tools
  428. CONVERT.TPU     Some convertion tools used by mapedit.pas
  429. PALETTE.TPU     Graphics initialization used by mapedit.pas
  430. BOX.TPU         Graphics utilities for popup boxes
  431. CGAEXP.PAS      Sample program using MouseTools (CGA)
  432. CGAEXP.EXE      Run-able version of sample program
  433. MAPEDIT.PAS     Sample program using MouseTools (EGA)
  434. MAPEDIT.EXE     Run-able version of sample program
  435. CCMAP.PIC       Sample data for mapedit.exe
  436. SHAREWRE.TXT    Explaination of shareware distribution
  437. MOUSTOOL.DOC    This file, the MouseTools documentation
  438.